<?php
/**
 * File: polymerkeywords.php.
 * Author: zhouzhe@haibao.com
 * Editor: PhpStorm
 * Date: 16/8/3 下午2:01
 * Brief:
 */

namespace haibao\cms\data;

class PolymerKeywords extends \haibao\cms\data\BaseMysql {
	public function __construct() {
		parent::__construct ( '\haibao\cms\model\data\PolymerKeywords' );
	}
	
	public function getKeywordsByTagId($tagId){
		$sql = "select Id,Keyword from PolymerKeywords where Status=1 and IsProduced=0 and ParentTagId=%s";
		$result = $this->query($sql,array($tagId));
		$keywordsList = array();
		while($result->fetch_assoc()){
			$keywordsList[$result->getData('Id')] = $result->getData('Keyword');
		}
	
		return $keywordsList;
	}
	
	public function getKeywordsList(){

		$polymerCache = new \haibao\cms\cache\Polymer();
		$keywordsArr = $polymerCache->getKeywordList();
		if(empty($keywordsArr)){
			$sql = "select Id,Keyword from PolymerKeywords where Status=1 and IsProduced=0";
			$result = $this->query($sql);
			while($result->fetch_assoc()){
				$parentTagId = $result->getData('ParentTagId');
				if(!isset($keywordsArr[$parentTagId]))$keywordsArr[$parentTagId] = array();
				$temp = array();
				$temp['Id'] = $result->getData('Id');
				$temp['Keyword'] = $result->getData('Keyword');
				array_push($keywordsArr[$parentTagId],$temp);
			}
			$polymerCache->setKeywordList($keywordsArr);
		}
		
		return $keywordsArr;
	}
	
	public function delKeywordCache(){
		$polymerCache = new \haibao\cms\cache\Polymer();
		$polymerCache->delKeywordList();
	}
	

	public function getKeywordsByName($keyword){
		$sql = "select Id,Keyword from PolymerKeywords where Keyword=%s limit 1";
		return $this->query($sql,array($keyword))->fetch_assoc();
	}
}